﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CommonLibrary.DataModel.Plan;
using ServiceApplication.Dao.Plan;
using System.Data;
using CommonLibrary.Common;
using CommonLibrary.DataModel.Basic;
using ServiceApplication.Service.Basic;

namespace ServiceApplication.Service.Plan
{
    public class PlanService
    {
        GroupDao groupDao = new GroupDao();
        NotifyDao notifyDao = new NotifyDao();

        #region group
        public int AddGroup(GroupEntity groupEntity)
        {
            return groupDao.AddEntity(groupEntity);
        }

        public void DeleteGroup(int group_id)
        {
            groupDao.DeleteEntity(group_id);
        }

        public void UpdateGroup(GroupEntity groupEntity)
        {
            groupDao.UpadateEntity(groupEntity);
        }

        public GroupEntity QueryGroup(int group_id)
        {
            return groupDao.QueryEntity(group_id);
        }

        public GroupEntity QueryGroup(int creater_id, string group_name)
        {
            return groupDao.QueryEntity(creater_id, group_name);
        }

        public Node QueryGroupTreeByCreaterId(int creater_id)
        {
            BasicService bs = new BasicService();
            DataTable dt = QueryGroupByCreaterId(creater_id);
            Node tree = new Node("我的小组", "我的小组", false, false);

            foreach (DataRow dr in dt.Rows)
            {
                Node childNode = new Node(dr["名称"].ToString(), dr["编码"].ToString(), false, false);
                string temp = dr["成员"].ToString();
                string[] users = temp.Split(',');
                foreach (var item in users)
                {
                    if (!string.IsNullOrEmpty(item))
                    {
                        UserEntity ue = bs.QueryUser(int.Parse(item));
                        childNode.Nodes.Add(new Node(ue.user_name, ue.user_id.ToString(), false, true));
                    }
                }
                tree.Nodes.Add(childNode);
            }
            return tree;
        }

        public DataTable QueryGroupByCreaterId(int creater_id)
        {
            return groupDao.QueryByCreaterId(creater_id);
        }


        #endregion

        #region notify
        public int AddNotify(NotifyEntity notifyEntity)
        {
            return notifyDao.AddEntity(notifyEntity);
        }

        public void DeleteNotify(int notify_id)
        {
            notifyDao.DeleteEntity(notify_id);
        }

        public NotifyEntity QueryNotify(int notify_id)
        {
            return notifyDao.QueryEntity(notify_id);
        }

        public void UpdateNotify(NotifyEntity notifyEntity)
        {
            notifyDao.UpadateEntity(notifyEntity);
        }

        public DataTable QueryAllNotify()
        {
            return notifyDao.QueryAll();
        }

        /// <summary>
        /// 查询最新的一条通知
        /// </summary>
        /// <returns></returns>
        public NotifyEntity QueryLastNotify()
        {
            return notifyDao.QueryLast();
        }
        #endregion
    }
}
